package cn.silver.module.authority.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.core.utils.file.MimeTypeUtils;
import cn.silver.framework.core.validate.auth.PasswordGroup;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.openapi.constant.ResultEnum;
import cn.silver.framework.openapi.model.Result;
import cn.silver.framework.satoken.utils.LoginHelper;
import cn.silver.framework.web.controller.ApiController;
import cn.silver.module.authority.model.AvatarVo;
import cn.silver.module.authority.param.SysUserProfileBo;
import cn.silver.module.system.bean.UserBean;
import cn.silver.module.system.param.PasswordParam;
import cn.silver.module.system.param.UserParam;
import cn.silver.module.system.service.ISysUserPostService;
import cn.silver.module.system.service.ISysUserRoleService;
import cn.silver.module.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * 个人信息 业务处理
 *
 * @author Silver Zhou
 */
@Validated
@SaCheckLogin
@RestController
@RequiredArgsConstructor
@Tag(name = "权限管理-个人信息管理")
@RequestMapping("/system/profile")
public class SysProfileController extends ApiController {

    private final ISysUserService userService;
    private final ISysUserRoleService userRoleService;
    private final ISysUserPostService userPostService;

    /**
     * 个人信息
     */
    @GetMapping
    public Result<UserBean> profile() {
        UserBean user = userService.selectById(LoginHelper.getUserId());
//        ProfileVo profileVo = new ProfileVo();
//        profileVo.setUser(user);
//        profileVo.setRoleGroup(userRoleService.selectRoleGroup(user.getId()));
//        profileVo.setPostGroup(userPostService.selectPostGroup(user.getCode()));
        return Result.ok(user);
    }

    /**
     * 修改用户
     */
    @PutMapping
    @Log(title = "个人信息", operate = OperateEnums.ALTER_UPDATE)
    public Result<Void> updateProfile(@RequestBody UserBean profile) {
        profile.setId(LoginHelper.getUserId());
        if (userService.update(profile)) {
            return Result.ok();
        }
        return Result.fail("修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", operate = OperateEnums.ALTER_UPDATE)
    @PutMapping("/pwd")
    public Result<Void> updatePwd(@Validated(PasswordGroup.class) @RequestBody PasswordParam param) {
        this.userService.updatePwd(param);
        return Result.fail("修改密码异常，请联系管理员");
    }

    /**
     * 头像上传
     *
     * @param avatarfile 用户头像
     */
    @Log(title = "用户头像", operate = OperateEnums.OSS_UPLOAD)
    @PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<AvatarVo> avatar(@RequestPart("avatarfile") MultipartFile avatarfile) {
        if (!avatarfile.isEmpty()) {
            String extension = FileUtil.extName(avatarfile.getOriginalFilename());
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                return Result.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
//            SysOssVo oss = ossService.upload(avatarfile);
//            String avatar = oss.getUrl();
//            if (userService.updateUserAvatar(LoginHelper.getUserId(), oss.getOssId())) {
//                AvatarVo avatarVo = new AvatarVo();
//                avatarVo.setImgUrl(avatar);
//                return Result.ok(avatarVo);
//            }
        }
        return Result.fail(ResultEnum.OSS_UPLOAD_ERROT.getCode(), "上传图片异常，请联系管理员");
    }
}
